package com.ylkj.patient.utils;

import android.app.Activity;
import android.widget.Toast;

import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.yilijk.base.utils.ALog;
import com.ylkj.patient.entity.WXpayData;


/**
 * 支付工具类  (微信)
 *
 * @author 刘有明 2017年1月16日
 */
public class PayUtils {

    /**
     * 调用微信SDK发起支付
     */
    public static void weixin(Activity mContext, WXpayData wXPayData) {
        if (mContext == null || mContext.isFinishing() || mContext.isDestroyed()){
            return;
        }

        // 微信支付相关
        IWXAPI mWxApi;
        StringBuffer mPrepayBuffer;
        mWxApi = WXAPIFactory.createWXAPI(mContext, "wxf401b8e4329a99f4");
        if (!mWxApi.isWXAppInstalled()) {
            //提醒用户没有按照微信
            Toast.makeText(mContext, "请安装微信", Toast.LENGTH_SHORT).show();
            return;
        }
        mWxApi.registerApp("wxf401b8e4329a99f4");
        PayReq request = new PayReq();
        request.appId = "wxf401b8e4329a99f4";//子商户appid
//        request.partnerId = "1601590907";//子商户号
        request.partnerId = wXPayData.getPartnerid();//子商户号
        request.prepayId = wXPayData.getPrepayId();
        request.packageValue = "Sign=WXPay";
        request.nonceStr = wXPayData.getNonceStr();
        request.timeStamp = wXPayData.getTimeStamp();
        request.sign = wXPayData.getSign();
        ALog.e("chen", "准备微信支付");
        ALog.e("oakkk", "oak"
                + "\nappid : " + request.appId
                + "\npartnerId : " + request.partnerId
                + "\nprepayId : " + request.prepayId
                + "\npackageValue : " + request.packageValue
                + "\nnonceStr : " + request.nonceStr
                + "\ntimeStamp : " + request.timeStamp
                + "\nsign : " + request.sign
        );
        mWxApi.sendReq(request);
    }
}
